package model

import (
	"gpt-api/framework/db"
)

type EventType struct {
	db.BaseModel
	Name   string `json:"name" gorm:"column:name;type:varchar(50);default:'';comment:事件类型名称"`
	UserID uint   `json:"user_id" gorm:"column:user_id;type:int;default:0;comment:用户ID"`
}

func (EventType) TableName() string {
	return "event_types"
}

// 根据ID获取事件类型
func (m EventType) GetEventType(database *db.DB, id uint) (EventType, error) {
	var eventType EventType
	err := database.Model(&EventType{}).Where("id = ?", id).First(&eventType).Error
	return eventType, err
}

// 根据用户ID获取事件类型列表
func (m EventType) GetEventTypes(database *db.DB, userId uint) ([]EventType, error) {
	var eventTypes []EventType
	// 添加默认userId
	userIds := []uint{0, userId}
	err := database.Model(&EventType{}).Where("user_id IN ?", userIds).Find(&eventTypes).Error
	return eventTypes, err
}

// 添加事件类型
func (m EventType) Create(database *db.DB) error {
	return database.Model(&EventType{}).Create(&m).Error
}

// 更新事件类型
func (m EventType) Update(database *db.DB) error {
	return database.Model(&EventType{}).Where("id = ?", m.ID).Updates(m).Error
}

// 删除事件类型
func (m EventType) Delete(database *db.DB) error {
	return database.Model(&EventType{}).Where("id = ?", m.ID).Delete(&m).Error
}
